// @ts-nocheck
import Vue from "vue";
import { Toast, Notify, Field } from "vant";
Vue.component(Field.name, {
  extends: Field,
  props: {
    // formatter: {
    //   type: Function,
    //   default: function (value) {
    //     return value.replace(/\s+/g, "");
    //   }
    // }
  },
});

Vue.prototype.$validateTime = validateTime;
Vue.prototype.$inputCheck = inputCheck;

//name 字段名称  value 字段值  isRequired是否必填 len最大长度 pattern自定义校验
// @input='paramsData.jcjg=$inputCheck(paramsData.jcjg,100,"content")'

function validateTime(param) {
  let { beignName, endName, beignValue, endValue } = param;
  if (!beignName) {
    beignName = "开始时间";
  }

  if (!endName) {
    endName = "结束时间";
  }
  if (!beignValue) {
    return true;
  }
  if (!endValue) {
    return true;
  }
  console.log(param);
  beignValue = beignValue.replace(/-/g, "/");
  endValue = endValue.replace(/-/g, "/");
  console.log(new Date(beignValue).getTime() > new Date(endValue).getTime());
  if (new Date(beignValue).getTime() > new Date(endValue).getTime()) {
    Toast(beignName + "不能晚于" + endName);
    return false;
  }
  return true;
}
//温度 湿度 四位数，两位小数 校验
export const WdSdCheckNumber = (value) => {
  let values =
    ("" + value)
      .replace(/[^\d\.]+/g, "")
      .replace(/\s/g, "")
      .replace(/^0+(\d)/, "$1")
      .replace(/^\./, "0.")
      .match(/^\d*(\.?\d{0,2})/g)[0] || "";
  if (values >= 9999) {
    let t = values.substring(0, 4);
    return t;
  } else {
    return values;
  }
};
//纯数字去除后2位为0 校验 请在页面用maxlength按要求限制相关长度
export const inputCheckNumber = (value) => {
  let values =
    ("" + value)
      .replace(/[^\d\.]+/g, "")
      .replace(/\s/g, "")
      .replace(/^0+(\d)/, "$1")
      .replace(/^\./, "0.")
      .match(/^\d*(\.?\d{0,4})/g)[0] || "";
  if (values >= 999999) {
    let t = values.substring(0, 6);
    return t;
  } else {
    return values;
  }
};
export const inputCheckNumber_1 = (value) => {
  let values =
    ("" + value)
      .replace(/[^\d\.]+/g, "")
      .replace(/\s/g, "")
      .replace(/^0+(\d)/, "$1")
      .replace(/^\./, "0.")
      .match(/^\d*(\.?\d{0,1})/g)[0] || "";
  if (values >= 999999) {
    let t = values.substring(0, 6);
    return t;
  } else {
    return values;
  }
};
export const inputCheckIntNumber = (value) => {
  let values =
    ("" + value)
      .replace(/[^\d\.]+/g, "")
      .replace(/\s/g, "")
      .replace(/^0+(\d)/, "$1")
      .replace(/^\./, "0.")
      .match(/^\d*(\.?\d{0})/g)[0] || "";
  values = values.replace(".", "");
  if (values >= 999999) {
    let t = values.substring(0, 6);
    return t;
  } else {
    return values;
  }
};

function inputCheck(value, maxLen = 40, type, pattern) {
  console.log("inputChecktype", type);
  let name = "";
  if (!pattern) {
    //默认只能输入中英文数字
    pattern = /[^\u4E00-\u9FA5A-Za-z\d]/g;
  }
  if (type == "content") {
    pattern = /[^\u4E00-\u9FA5A-Za-z\d，。,.;、：；（）()#ⅠⅡⅢⅣⅤ+-]/g;
  } else if (type == "dept") {
    pattern = /[^\u4E00-\u9FA5A-Za-z\d （）()_——#-]/g;
  } else if (type == "pmsContent") {
    pattern = /[^\u4E00-\u9FA5A-Za-z\d，。,.;、：:；（）~～()【】#ⅠⅡⅢⅣⅤ [\]-]/g;
  } else if (type == "line") {
    //设备
    pattern = /[^\u4E00-\u9FA5A-Za-z\d#ⅠⅡⅢⅣⅤ（）()+-]/g;
  } else if (type == "model") {
    //型号
    pattern = /[^\u4E00-\u9FA5A-Za-z\d，。（）()#ⅠⅡⅢⅣⅤ-]/g;
  } else if (type == "znsb") {
    //型号 znsb
    pattern = /[^\u4E00-\u9FA5A-Za-z\d，。,.;、：；【】[]（）()#ⅠⅡⅢⅣⅤ-]/g;
  } else if (type == "number") {
    //数字温度之类可负数
    const newValue = WdSdCheckNumber(value);
    if (newValue !== value) {
      return newValue;
    }
    pattern = undefined;
  } else if (type == "positiveNumber") {
    //大于0的数字
    const newValue = inputCheckNumber(value);
    if (newValue !== value) {
      return newValue;
    }
    pattern = undefined;
  } else if (type == "positiveNumberFixed1") {
    //大于0的数字
    const newValue = inputCheckNumber_1(value);
    if (newValue !== value) {
      return newValue;
    }
    pattern = undefined;
  } else if (type == "intNumber") {
    //大于等0的数字
    const newValue = inputCheckIntNumber(value);
    if (newValue !== value) {
      return newValue;
    }
    pattern = undefined;
  } else if (type == "phone") {
    pattern = /[^0-9]/g;
  }

  //判断有没有包含null（包括大小写）
  var patt = new RegExp("null", "ig");
  var resultnull = value ? patt.test(value) : false;
  if (value && resultnull) {
    Toast(name + "不能包含null（包括大小写）！", "error");
    return value.replace(patt, "");
  }

  if (value && pattern && pattern.test(value)) {
    // Toast(name + "不能包含特殊字符！", "error");
    return value.replace(pattern, "");
  }
  //判断有没有包含禁止输入关键词
  let disInputList = [
    "onload",
    "onclick",
    "onfocus",
    "onblur",
    "onmouseover",
    "onerror",
    "confirm",
    "getParameter",
    "getRequestURI",
    "eval",
    "or",
    "and",
  ];
  let disfilter = [];
  if (value) {
    disfilter = disInputList.filter((item) => {
      return value.includes(item);
    });
  }
  if (disfilter.length > 0) {
    Toast(name + "不允许输入" + disfilter[0], "error");
    disfilter.forEach((i) => {
      value = value.replace(new RegExp(i, "g"), "");
    });
    return value;
  }

  //判断有没有长度超过
  if (value && value.length > maxLen) {
    Toast(name + "长度不能超过" + maxLen + "字符！", "error");
    return value.substring(0, maxLen);
  }
  return value;
}
